package com.opensource.component.query.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.opensource.component.query.matedata.TableMetadata;
import com.opensource.component.query.matedata.TableMetadataHelper;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author ZonLen since on 2021/12/22 上午9:25
 */
public abstract class AbstractExtendMethod extends AbstractMethod implements ExtendMethod {

  @Override
  public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> tableClass,
      TableInfo tableInfo) {
    return mappedStatementSupplierForOther(mapperClass, tableClass, tableInfo);
  }

  @Override
  public MappedStatement mappedStatementSupplierForOther(Class<?> mapperClass, Class<?> tableClass,
      TableInfo tableInfo) {
    TableMetadataHelper.addTableMetadata(new TableMetadata(tableInfo));
    SqlSource sqlSource = languageDriver
        .createSqlSource(configuration, sqlFormat(tableInfo), tableClass);
    return this
        .addSelectMappedStatementForOther(mapperClass, sqlCommandType().getMethod(), sqlSource,
            resultType(tableInfo));
  }

}
